import { RouterRecord } from "@/global/record/RouterRecord";

export const useTagsViewStore = defineStore(
  'tagsView',
  () => {
    const visitedViews = ref<RouterRecord[]>([]);

    const cachedViews = ref<string[]>([]);

    const iframeViews = ref<RouterRecord[]>([]);

    function addView(view: RouterRecord) {
      addVisitedView(view)
      addCachedView(view)
    }

    function addVisitedView(view: RouterRecord) {
      if (visitedViews.value.some(v => v.path === view.path)) return
      visitedViews.value.push(
        Object.assign({}, view, {})
      )
    }

    function addIframeView(view: RouterRecord) {
      if (iframeViews.value.some(v => v.path === view.path)) return
      iframeViews.value.push(
        Object.assign({}, view, {})
      )
    }

    function addCachedView(view: RouterRecord) {
      if (cachedViews.value.includes(view.name)) return
      if (!view.meta.noCache) {
        cachedViews.value.push(view.name)
      }
    }

    function delView(view: RouterRecord) {
      delVisitedView(view)
      delCachedView(view)
      return {
        visitedViews: [...visitedViews.value],
        cachedViews: [...cachedViews.value]
      }
    }

    function delVisitedView(view: RouterRecord) {
      for (const [i, v] of visitedViews.value.entries()) {
        if (v.path === view.path) {
          visitedViews.value.splice(i, 1)
          break
        }
      }
      iframeViews.value = iframeViews.value.filter(item => item.path !== view.path)
      return [...visitedViews.value];
    }

    function delCachedView(view: RouterRecord) {
      const index = cachedViews.value.indexOf(view.name)
      index > -1 && cachedViews.value.splice(index, 1)
      return [...cachedViews.value]
    }

    function delOthersViews(view: RouterRecord) {
      delOthersVisitedViews(view)
      delOthersCachedViews(view)
      return {
        visitedViews: [...visitedViews.value],
        cachedViews: [...cachedViews.value]
      }
    }

    function delOthersVisitedViews(view: RouterRecord) {
      visitedViews.value = visitedViews.value.filter(v => {
        return v.meta.affix || v.path === view.path
      })
      iframeViews.value = iframeViews.value.filter(item => item.path === view.path)
      return [...visitedViews.value];
    }

    function delOthersCachedViews(view: RouterRecord) {
      const index = cachedViews.value.indexOf(view.name)
      if (index > -1) {
        cachedViews.value = cachedViews.value.slice(index, index + 1)
      } else {
        cachedViews.value = []
      }
      return [...cachedViews.value]
    }

    function updateVisitedView(view: RouterRecord) {
      for (let v of visitedViews.value) {
        if (v.path === view.path) {
          v = Object.assign(v, view)
          break
        }
      }
    }

    function delLeftTags(view: RouterRecord) {
      const index = visitedViews.value.findIndex(v => v.path === view.path)
      if (index === -1) {
        return
      }
      visitedViews.value = visitedViews.value.filter((item, idx) => {
        if (idx >= index || (item.meta && item.meta.affix)) {
          return true
        }
        const i = cachedViews.value.indexOf(item.name)
        if (i > -1) {
          cachedViews.value.splice(i, 1)
        }
        if (item.meta.link) {
          const fi = iframeViews.value.findIndex(v => v.path === item.path)
          iframeViews.value.splice(fi, 1)
        }
        return false
      })
      return [...visitedViews.value];
    }

    function delRightTags(view: RouterRecord) {
      const index = visitedViews.value.findIndex(v => v.path === view.path)
      if (index === -1) {
        return
      }
      visitedViews.value = visitedViews.value.filter((item, idx) => {
        if (idx <= index || (item.meta && item.meta.affix)) {
          return true
        }
        const i = cachedViews.value.indexOf(item.name)
        if (i > -1) {
          cachedViews.value.splice(i, 1)
        }
        if (item.meta.link) {
          const fi = iframeViews.value.findIndex(v => v.path === item.path)
          iframeViews.value.splice(fi, 1)
        }
        return false
      })
      return [...visitedViews.value]
    }

    function delAllViews() {
      delAllVisitedViews()
      delAllCachedViews()
      return {
        visitedViews: [...visitedViews.value],
        cachedViews: [...cachedViews.value]
      }
    }

    function delAllVisitedViews() {
      visitedViews.value = visitedViews.value.filter(tag => tag.meta.affix);
      iframeViews.value = []
      return [...visitedViews.value]

    }

    function delAllCachedViews() {
      cachedViews.value = []
      return [...cachedViews.value]
    }

    return {
      visitedViews,
      cachedViews,
      iframeViews,
      addView,
      addVisitedView,
      addIframeView,
      delView,
      delVisitedView,
      delCachedView,
      delOthersViews,
      updateVisitedView,
      delLeftTags,
      delRightTags,
      delAllViews,
    }
  }
)